
<template>
    <el-page-header icon="el-icon-arrow-left" @back="$router.replace('/room')" content="房间管理"></el-page-header>
    <el-divider></el-divider>
    <el-form :model="formData" ref="myForm" :rules="rules" label-width="100px">
        <el-form-item label="房间编号" prop="roomNum">
                <el-input v-model="formData.roomNum" placeholder="如: 1001"></el-input>
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
                <el-input-number v-model="formData.floor"></el-input-number>
        </el-form-item>
        <el-form-item label="楼栋代码" prop="buildingCode">
            <el-input v-model="formData.buildingCode" placeholder="如: A"></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="flag">
            <el-switch v-model="formData.flag" :active-value=1 :inactive-value=0 active-text="启用" inactive-text="禁用"></el-switch>
        </el-form-item>
        <el-form-item label="所属房型" prop="roomTypeId">
            <el-select v-model="formData.roomTypeId" filterable placeholder="请选择">
                <el-option label="请选择" :value="-1"></el-option>
                <el-option
                        v-for="item in roomTypeOptions"
                        :key="item.id"
                        :label="item.typeName"
                        :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm('myForm')">提交</el-button>
            <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script>

    import { ElMessage } from 'element-plus'

    export default {
        data() {
            return {
                isUpdate: false,
                formData: {
                    id: '',
                    roomNum: '',
                    floor: 1,
                    buildingCode: '',
                    flag: 1,
                    roomTypeId: -1
                },
                roomTypeOptions: [],
                rules: {
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (!valid) return false;
                    // 发送网络请求
                    if (this.isUpdate) {    // 更新
                        this.axios.put('room', this.formData)
                            .then(resp => {
                                if (!resp.data.flag) {
                                    ElMessage.error(resp.data.msg);
                                    return;
                                }
                                ElMessage.success(resp.data.msg);
                                this.$router.replace('/room');
                            });
                    } else {    // 新增
                        this.axios.post('room', this.formData)
                            .then(resp => {
                               if (!resp.data.flag) {
                                   ElMessage.error(resp.data.msg);
                                   return;
                               }
                                ElMessage.success(resp.data.msg);
                                this.$router.replace('/room');
                            });
                    }
                });
            }
        },
        created() {
            let id = this.$route.query.id;
            if (id > 0) {
                this.isUpdate = true;
                this.axios.get('room/' + id)
                .then(resp => {
                    if (!resp.data.flag) {
                        ElMessage.error(resp.data.msg);
                        return;
                    }
                    this.formData = resp.data.data.room;
                });
            }
            // 查询房型列表
            this.axios.get('roomtype/findOptions')
                .then(resp => {
                    if (!resp.data.flag) {
                        ElMessage.error(resp.data.msg);
                        return;
                    }
                    this.roomTypeOptions = resp.data.data.list;
                });
        }
    }
</script>

<style scoped>

</style>